package com.xiaochun.jiankangapp.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.reflect.TypeToken
import com.xiaochun.jiankangapp.model.BodyInfoChatModel
import com.xiaochun.jiankangapp.model.BodyInfoItemModel
import com.xiaochun.jiankangapp.model.Response
import com.xiaochun.jiankangapp.model.UserInfo
import com.xiaochun.jiankangapp.network.Request
import kotlinx.coroutines.launch

class BodyInfoViewModel : ViewModel() {

    fun save(model: BodyInfoChatModel, callback: (Response<BodyInfoItemModel>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<BodyInfoItemModel>(
                "api/refer/add",
                mapOf("type" to model.type, "content" to model.content)
            )
            callback.invoke(r)
        }
    }

    fun getList(callback: (Response<List<BodyInfoItemModel>>) -> Unit) {
        viewModelScope.launch {
            val r = getList()
            callback.invoke(r)
        }
    }

    suspend fun getList(): Response<List<BodyInfoItemModel>> {
        return Request.postSuspend<List<BodyInfoItemModel>>(
            "api/refer/details", type = object : TypeToken<List<BodyInfoItemModel>>() {

            }.type
        )
    }
}